﻿using Drive.LiveChat.Model;
using Drive.LiveChat.Server;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace Drive.LiveExcelHost
{
    public class MessageForThrottle
    {
        public long UserId { get; set; }
        public DateTime SendTime { get; set; }
    }

    public enum ChatEventType
    {
        /// <summary>
        /// 系统消息
        /// </summary>
        SYSTEM = 0,
        /// <summary>
        /// 会话就绪
        /// </summary>
        SESSION_READY = 1,
        /// <summary>
        /// 欢迎消息
        /// </summary>
        WELCOME_ENTER = 2,
        /// <summary>
        /// 某人进入
        /// </summary>
        BUDDY_ENTER = 4,
        /// <summary>
        /// 某人离开
        /// </summary>
        BUDDY_LEAVE = 5,

        /// <summary>
        /// 消息发送太快
        /// </summary>
        SEND_THROTTLE = 6,

        /// <summary>
        /// 最近历史消息
        /// </summary>
        CHAT_HISTORY = 7,

        /// <summary>
        /// 消息写入完成
        /// </summary>
        CHAT_PERSIST = 8,

        /// <summary>
        /// 群聊
        /// </summary>
        GROUP_CHAT = 100,
        /// <summary>
        /// 私聊
        /// </summary>
        BUDDY_CHAT = 101,
    }
    public class TypedMessage
    {
        public object content { get; set; }

        public ChatEventType type { get; set; }

        public string typeName
        {
            get
            {
                return this.type.ToString();
            }
        }

        public static string CreateThrottle(string content)
        {
            return JSON.Stringify(new TypedMessage
            {
                type = ChatEventType.SEND_THROTTLE,
                content = content,
            });
        }

        public static string CreateHistory(IEnumerable<object> messages)
        {
            return JSON.Stringify(new TypedMessage
            {
                type = ChatEventType.CHAT_HISTORY,
                content = messages,
            });
        }

        public static string CreateWelcome(string welcome)
        {
            return JSON.Stringify(new TypedMessage
            {
                type = ChatEventType.WELCOME_ENTER,
                content = welcome,
            });
        }

        public static string CreateSessionReady(ChatBuddy from, object d)
        {
            return JSON.Stringify(new TypedMessage
            {
                type = ChatEventType.SESSION_READY,
                content = d,
            });
        }

        public static string CreateBuddyLeave(ChatBuddy buddy, object d)
        {
            return JSON.Stringify(new TypedMessage
            {
                content = d,
                type = ChatEventType.BUDDY_LEAVE,
            });
        }

        public static string CreateBuddyEnter(ChatBuddy buddy, object d)
        {
            return JSON.Stringify(new TypedMessage
            {
                content = d,
                type = ChatEventType.BUDDY_ENTER,
            });
        }

        public static string CreateSystem(string content)
        {
            return JSON.Stringify(new TypedMessage
            {
                content = content,
                type = ChatEventType.SYSTEM,
            });
        }



        public static string CreateGroupChat(ChatBuddy from, string content)
        {
            return JSON.Stringify(new TypedMessage
            {
                content = content,
                type = ChatEventType.GROUP_CHAT,
            });
        }

        public static string CreateChatPersist(long sendId, long writeId)
        {
            return JSON.Stringify(new TypedMessage
            {
                type = ChatEventType.CHAT_PERSIST,
                content = new
                {
                    sendId = sendId,
                    id = writeId
                }
            });
        }

        public static string CreateMessageFromVisitor(ChatBuddy from, ChatMessage message, Conversation conv)
        {
            return JSON.Stringify(new TypedMessage
            {
                content = new { message = message, conv = conv },
                type = ChatEventType.BUDDY_CHAT
            });
        }

        public static string CreateMessageFromAgent(ChatMessage message, Conversation conv)
        {
            return JSON.Stringify(new TypedMessage
            {
                content = new { message = message, conv = conv },
                type = ChatEventType.BUDDY_CHAT
            });
        }
    }
    public class ChatBuddy
    {
        public string SessionId { get; set; }
        public long VisitorId { get; set; }
        public string NickName { get; set; }
    }
}
